package com.cssw.fyzb.busi.api.acct;

import com.cssw.fyzb.core.model.vo.acct.UserBankVo;
import com.cssw.fyzb.core.exception.BizException;
import com.cssw.fyzb.busi.service.acct.OutAcctService;
import com.cssw.fyzb.core.model.dodb.UserBank;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: yangyiqun
 * @Date: 2020/7/11 16:25
 */
@Function(description = "查询默认银行卡信息", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true)
})
@Component
public class QueryBankPay implements Action {
    private static final Logger logger = LoggerFactory.getLogger(QueryBankPay.class);

    @Autowired
    private OutAcctService outAcctService;

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        try {
            UserBank userBank = outAcctService.getBankByUserId(userId);
            UserBankVo userBankVo = null;
            if (userBank != null) {
                userBankVo = new UserBankVo();
                BeanUtils.copyProperties(userBank, userBankVo);
                userBankVo.setCarNo(userBank.getCardNo());
            }
            return Response.success().put("data", userBankVo);
        } catch (BizException e) {
            return Response.fail(e.getErrMsg(), e.getErrCode());
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("查询默认银行卡信息失败");
        }
    }
}
